﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class PromotionDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Get all Promotion in Order By PromotionName
        /// </summary>
        /// <author>vuongvt</author>
        /// <returns>List of Promotion</returns>
        public List<Promotion> GetAllPromotions()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var promotions = from promotion in _entities.Promotions
                                 where promotion.IsDeleted == false
                                 orderby promotion.PromotionName
                                 select promotion;
                if (promotions != null)
                {
                    listPromotion = promotions.ToList();
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list of Promotions Order By PromotionId
        /// </summary>
        /// <returns>List of promotions</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public List<Promotion> LoadPromotionsOrderById()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var promotions = from promotion in _entities.Promotions
                                 where promotion.IsDeleted == false
                                 orderby promotion.PromotionId descending
                                 select promotion;
                if (promotions != null)
                {
                    listPromotion = promotions.ToList();
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get a promotion by Promotion Id
        /// </summary>
        /// <returns>Promotion</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public Promotion LoadPromotionById(int id)
        {
            try
            {
                Promotion promotion = new Promotion();
                promotion = (from promotionEnt in _entities.Promotions
                             where promotionEnt.PromotionId == id
                                   && promotionEnt.IsDeleted == false
                             select promotionEnt).FirstOrDefault();
                return promotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add a promotion
        /// </summary>
        /// <returns>int</returns>
        /// <author>VuongVT</author>
        /// <date>3/7/2014</date>
        public int Add(Promotion promotion)
        {
            try
            {
                _entities.Promotions.Add(promotion);
                return _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit a Film
        /// </summary>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        public int Edit(Promotion promotion)
        {
            try
            {
                _entities.Entry(promotion).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// delete promotion
        /// </summary>
        /// <returns>int</returns>
        /// <author>hungph</author>
        /// <date>3/7/2014</date>
        public int Delete(Promotion promotion)
        {
            try
            {
                promotion.IsDeleted = true;
                _entities.Entry(promotion).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if promotion name is existed or not
        /// </summary>
        /// <param name="promotion"></param>
        /// <returns>true || false</returns>
        /// <author>hungph</author>
        /// <date>3/7/2014</date>
        public bool CheckIsExisted(Promotion promotion)
        {
            try
            {
                var isExist = _entities.Promotions.Any(c => c.IsDeleted == false && c.PromotionName == promotion.PromotionName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}